package sample;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.Pane;
import javafx.scene.text.Text;

import java.io.IOException;
import java.net.URL;
import java.sql.SQLException;

import javafx.stage.Stage;
import javafx.scene.control.TextField;
import javafx.scene.control.*;

public class Controller<listView, WbView> {

    @FXML
    public Button mybutton;
    public static Message message=null;
    public static oilMessage oil=null;
    public static Stage st = null;
    // 与fxml_example.fxml文件中的登录提示框fx:id一致
        @FXML private Text actiontarget;
        @FXML private TextField mytext;
        @FXML private Pane pane;
    @FXML private TableColumn name;
    @FXML private TableColumn conNum;
    @FXML private TableColumn deathNum;
    @FXML private TableColumn cureNum;
    @FXML private TableColumn asymptomNum;
    @FXML private TableColumn updateTime;

    @FXML private TableView table;

        MessageDao dn = new MessageDao();
        oilDao oildao = new oilDao();
        // 为fxml_example.fxml文件中的登录按钮编写事件处理
        @FXML protected void handleSubmitButtonAction(ActionEvent event) {
            actiontarget.setText("点击了登录按钮");

    }
        @FXML protected void MyButton (ActionEvent event) throws SQLException, IOException {
            mybutton.setOnMouseClicked(new EventHandler<MouseEvent>() {
                @Override
                public void handle(MouseEvent event) {

                }
            });
            if (mytext.getText().trim().equals("") ) {
                // 显示对话框

                Alert alert = new Alert(Alert.AlertType.WARNING);
                alert.setHeaderText("输入为空查询失败！");
                alert.setContentText("请重新输入检索关键词");
                alert.show();
            }else if(dn.isExistCity(mytext.getText().trim())){
                Alert alert = new Alert(Alert.AlertType.WARNING);
                alert.setHeaderText("输入信息有误！");
                alert.setContentText("请重新输入！");
                alert.show();
            }
            else{

                String cityName="";
                cityName = mytext.getText().trim();
                 message=dn.getSqlConnection(cityName);
                URL url = new URL("file:src/sample/fxm3.fxml");

                try {
                    Scene scene = new Scene(FXMLLoader.load(url));
                    Stage stage1 = new Stage();
                    stage1.setScene(scene);
                    st = stage1;
                    stage1.show();

                } catch (IOException e) {
                    throw new RuntimeException(e);
                }


                    }
                };
        @FXML
    public  Button mybutton2;
        public TextField mytext2;
    @FXML protected void MyButton2 (ActionEvent event) throws SQLException, IOException {
        if (mytext2.getText().trim().equals("") ) {
            // 显示对话框

            Alert alert = new Alert(Alert.AlertType.WARNING);
            alert.setHeaderText("输入为空查询失败！");
            alert.setContentText("请重新输入检索关键词");
            alert.show();
        }else if(oildao.isExistCity(mytext2.getText().trim())){
            Alert alert = new Alert(Alert.AlertType.WARNING);
            alert.setHeaderText("输入信息有误！");
            alert.setContentText("请重新输入！");
            alert.show();
        }else{
            String cityName="";
            cityName = mytext2.getText().trim();
            oil=oildao.getSqlConnection(cityName);
            URL url = new URL("file:src/sample/fxm3.fxml");

            ObservableList<oilMessage> data = FXCollections.observableArrayList(oil);
            //表使用的数据
            TableView<oilMessage> table = new TableView();
            //定义表格的行标
            TableColumn name = new TableColumn("城市");
            TableColumn conNum = new TableColumn("p0油价");
            TableColumn deathNum = new TableColumn("p89油价");
            TableColumn cureNum = new TableColumn("p92油价");
            TableColumn asymptomNum = new TableColumn("p95油价");
            TableColumn updateTime = new TableColumn("数据更新时间");

            //表格列宽宽度设置
            name.setMinWidth(100);
            conNum.setMinWidth(100);
            deathNum.setMinWidth(100);
            cureNum.setMinWidth(100);
            asymptomNum.setMinWidth(100);
            updateTime.setMinWidth(180);

            //确定数据导入的列
            name.setCellValueFactory(
                    new PropertyValueFactory<>("province"));
            conNum.setCellValueFactory(
                    new PropertyValueFactory<>("p0"));
            deathNum.setCellValueFactory(
                    new PropertyValueFactory<>("p89"));
            cureNum.setCellValueFactory(
                    new PropertyValueFactory<>("p92"));
            asymptomNum.setCellValueFactory(
                    new PropertyValueFactory<>("p95"));
            updateTime.setCellValueFactory(
                    new PropertyValueFactory<>("update_time"));
            //向表中导入数据
            table.setItems(data);
            table.setStyle("-fx-background-color:red");
            table.getColumns().addAll(name, conNum, deathNum, cureNum,asymptomNum,updateTime);
            System.out.println(name.getColumns());
            Scene scene = new Scene(table,680, 200);
            Stage stage1 = new Stage();
            stage1.setScene(scene);
            stage1.show();
        }
    };
    @FXML
    public TextField textField;
    @FXML
    public ListView myList;

    @FXML
    public DataBilibili connectionAPI(){//从借口获取数据
        String id=textField.getText().trim();
        String url="https://qqlykm.cn/api/bilibili/get?key=BizFyNBxmboxrjOfSsAOWl92nc&id=" +id;
        String result= HttpUtil.get(url);
        DataBilibili data= JSONUtil.toBean(result, DataBilibili.class);
        return data;
    }

    @FXML
    protected void printArrayList() {//将数据读取到界面
        DataBilibili dataBilibili = connectionAPI();
        String text = textField.getText().trim();
        QueryBilibili queryBilibili = new QueryBilibili();
        ObservableList<Object> myListData = FXCollections.observableArrayList();
        queryBilibili.querybilibili(text, dataBilibili, myList, myListData);

    }
            }





